package model.algorithms.splitting;

import model.components.networks.network.VirtualNetwork;

public class IteratedLocalSearch extends SplittingAlgorithm
{
	public IteratedLocalSearch( String name )
	{
		super( name );
	}

	@Override
	protected int[] bestNodesMapping()
	{	
		VirtualNetwork virtualNetwork = this.getVirtualNetwork();
		int virtualNodesNumber = virtualNetwork.getNodes().size();
		int physicalNodesNumber = this.getPhysicalNetworks().size();
		int nodesMapping[] = new int[ virtualNodesNumber ];
			
		for( int i = 0; i < virtualNodesNumber; i++ )
			nodesMapping[ i ] = (int) (Math.random() * physicalNodesNumber);
		
		int trialsNumber = 10 * physicalNodesNumber * virtualNodesNumber;
		nodesMapping = this.nodesMapping( nodesMapping, trialsNumber );
		
		int maxExperimentsNumber = 5;
		double currentNetworkMappingCost = Integer.MAX_VALUE;
		int bestNodesMapping[] = new int[ virtualNodesNumber ];
		
		for( int g = 0; g < maxExperimentsNumber; g++ )
		{
			int perturbation = (int) Math.round( 0.8 * virtualNodesNumber );
			
			for( int i = 0; i < perturbation; i++ )
				nodesMapping[ i ] = (int) (Math.random() * physicalNodesNumber);
			
			nodesMapping = this.nodesMapping( nodesMapping, trialsNumber );
						
			double networkMappingCost = this.networkMappingCost( nodesMapping );
			
			if( networkMappingCost < currentNetworkMappingCost )
			{
				currentNetworkMappingCost = networkMappingCost;
				bestNodesMapping = nodesMapping.clone();
				
				//for( int i = 0; i < virtualNodesNumber; i++ )
					//bestNodesMapping[ i ] = nodesMapping[ i ];
			}
		}
		
		return bestNodesMapping;
	}
	
}